import { computed } from "vue";
import { useStore } from "@/store";
import type { Tabs } from "@model/views";

export default function () {
	const store = useStore();

	function getTabs<T = any>(key: string): T {
		return store.getters[`tabs/${key}`];
	}

	function setTabs<T = any>(key: string, value?: T) {
		store.dispatch(`tabs/${key}`, value);
	}
	const keepList = computed<Tabs[]>(() => getTabs("keepList"));

	const activeList = computed<Tabs[]>(() => getTabs("activeList"));

	function init() {
		setTabs("INIT_TABS");
	}

	function addTabs(value: Tabs) {
		setTabs("ADD_TABS", value);
	}

	function removeTabs(type: "this" | "all" | "other" | "left" | "right", index?: number) {
		let key;
		switch (type) {
			case "this":
				key = "REMOVE_TABS";
				break;
			case "all":
				key = "REMOVE_ALL_TABS";
				break;
			case "other":
				key = "REMOVE_OTHER_TABS";
				break;
			case "left":
				key = "REMOVE_LEFT_TABS";
				break;
			case "right":
				key = "REMOVE_RIGHT_TABS";
				break;
			default:
				key = "";
				break;
		}
		setTabs(key, index);
	}

	function keep(index: number) {
		setTabs("KEEP_TABS", index);
	}

	function removeKeep(index: number) {
		setTabs("REMOVE_KEEP_TABS", index);
	}

	return {
		keepList,
		activeList,
		init,
		addTabs,
		removeTabs,
		keep,
		removeKeep,
	};
}
